package com.gulimall.member.service.impl;

import com.common.to.FareTo;
import com.common.to.MemberAddressTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.gulimall.member.dao.MemberReceiveAddressDao;
import com.gulimall.member.entity.MemberReceiveAddressEntity;
import com.gulimall.member.service.MemberReceiveAddressService;

import javax.annotation.Resource;

@Slf4j
@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Resource
    private MemberReceiveAddressDao memberReceiveAddressDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MemberAddressTo> getAddressListByUserId(Long userId) {
        List<MemberReceiveAddressEntity> memberAddressList = memberReceiveAddressDao.selectList(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", userId));
        List<MemberAddressTo> memberAddressToList = memberAddressList.stream().map(memberReceiveAddressEntity -> {
            MemberAddressTo memberAddressTo = new MemberAddressTo();
            BeanUtils.copyProperties(memberReceiveAddressEntity, memberAddressTo);
            return memberAddressTo;
        }).collect(Collectors.toList());
        return memberAddressToList;
    }

    @Override
    public FareTo getFare(Long addressId) {
        MemberReceiveAddressEntity memberReceiveAddressEntity = memberReceiveAddressDao.selectById(addressId);
        String mobile = memberReceiveAddressEntity.getPhone();
        int i = Integer.parseInt(mobile.substring(mobile.length() - 1));
        FareTo fareTo = new FareTo();
        MemberAddressTo memberAddressTo = new MemberAddressTo();
        BeanUtils.copyProperties(memberReceiveAddressEntity,memberAddressTo);
        fareTo.setAddress(memberAddressTo);
        fareTo.setFare(new BigDecimal(i));
        return fareTo;
    }

}